package com.bawei.demo_shoppingcar.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.bawei.demo_shoppingcar.R;
import com.bawei.demo_shoppingcar.application.ContentData;
import com.bawei.demo_shoppingcar.fragment.FenLeiFragment;
import com.bawei.demo_shoppingcar.fragment.MineFragment;
import com.bawei.demo_shoppingcar.fragment.MyFragment;
import com.bawei.demo_shoppingcar.fragment.ShoppingCarFragment;
import com.bawei.demo_shoppingcar.utils.FragmentTabHost;

public class ContentActivity extends AppCompatActivity {


    private Class mFragment[] = {
            MyFragment.class, FenLeiFragment.class, ShoppingCarFragment.class, MineFragment.class
    };

    private int mTabImageView[] = {
            R.drawable.tab_home_btn, R.drawable.tab_classify_btn, R.drawable.tab_shopping_btn, R.drawable.tab_user_btn
    };


    private String[] mTitleArray = new String[]{
            "首页", "分类", "购物车", "个人中心"
    };
    private FragmentTabHost tabhost;
    private FrameLayout tab_fragment;
    private FrameLayout content_fragment;
    private ImageView tab_image;
    private TextView tab_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        initView();

        initData();
    }



    private void initData() {
        tabhost.setup(this,getSupportFragmentManager(),R.id.content_fragment);
        for (int i=0;i<mFragment.length;i++){
            TabHost.TabSpec tabSpec = tabhost.newTabSpec(mTitleArray[i]).setIndicator(getTabItmView(i));
            tabhost.addTab(tabSpec,mFragment[i],null);
        }
        tabhost.getTabWidget().setDividerDrawable(null);
        tabhost.getTabWidget().getChildTabViewAt(2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContentData.isLogin_QQ())
                tabhost.setCurrentTab(2);
                else {
                    startActivity(new Intent(getApplicationContext(), UserRegisterActivity.class));
                }
            }
        });
    }

    private void initView() {

        tabhost = (FragmentTabHost) findViewById(R.id.tabhost);
        tab_fragment = (FrameLayout) findViewById(R.id.tab_fragment);
        content_fragment = (FrameLayout) findViewById(R.id.content_fragment);

    }

    private View getTabItmView(int index){

        View inflate = LayoutInflater.from(this).inflate(R.layout.tab_item_view, null);
        tab_image = (ImageView) inflate.findViewById(R.id.tab_image);
        tab_text = (TextView) inflate.findViewById(R.id.tab_text);
        tab_image.setImageResource(mTabImageView[index]);
        tab_text.setText(mTitleArray[index]);

        return inflate;
    }
}
